Re: [SQL] PgSQL 6.5.1 and Group BY pb - Mailing list pgsql-sql

From Herouth Maoz
Subject Re: [SQL] PgSQL 6.5.1 and Group BY pb
Date
Msg-id l03130304b3e996ea9e0d@[147.233.159.109]
Whole thread Raw
In response to PgSQL 6.5.1 and Group BY pb  (Giampiero Raschetti <Giampiero.Raschetti@popso.it>)
Responses Re: [SQL] PgSQL 6.5.1 and Group BY pb  ("Albert REINER" <areiner@tph.tuwien.ac.at>)
List pgsql-sql
At 14:08 +0300 on 25/08/1999, Giampiero Raschetti wrote:


> And now the output query with GROUP BY:
>
> template1=> SELECT g.nome,u.uid,u.id FROM gruppi g, usergroup u GROUP BY
> g.nome;
> ERROR:  Illegal use of aggregates or non-group column in target list
> template1=> SELECT nome,id FROM gruppi GROUP BY nome;
> ERROR:  Illegal use of aggregates or non-group column in target list
>
> And now the output query with SELECT DISTINCT:
>
> template1=> SELECT DISTINCT ON g.nome g.nome,u.uid,u.id from gruppi g,
> usergroup u;
> ERROR:  parser: parse error at or near "."

I think you have a misunderstanding about the purpose of group by statements.

In fact, the above seems to indicate that you are not well aware of what
joins are, or at least you don't know that you have to restrict them to
make sense of your data. You really have to add WHERE g.id = u.id.

Anyway, what was it that you wanted to display in that second query of
yours? For each nome, display some id that matches it? Define "some". Which
id do you want. DON'T use "distinct on". It makes no sense. SQL is defined
in such a way that the returned set of results will be the same in all
implementations that respect the standard. If you use "DISTINCT ON", it
will pick up a representative at will, and it may not be the same
representative picked by other implementations. So... it is not a standard
part of SQL.

In order to be more standard, you have to tell it how to pick the
representative. For example, tell him to give you the smallest-number id
that matches a group. This is done with min():

SELECT g.nome, min(u.uid), min(u.id)
FROM gruppi g, usergroup u
WHERE g.id = u.id
GROUP BY g.nome;

This will work. The question is whether this is what you wanted.

Herouth

--
Herouth Maoz, Internet developer.
Open University of Israel - Telem project
http://telem.openu.ac.il/~herutma




pgsql-sql by date:

Previous
From: Howie
Date:
Subject: Re: [SQL] Dumping Oracle tables into Postgres
Next
From: Tom Lane
Date:
Subject: Re: [SQL] PgSQL 6.5.1 and Group BY pb